// SparseArray.cpp: implementation of the SparseArray class.
//
//////////////////////////////////////////////////////////////////////

#include "sparsearray.h"

//////////////////////////////////////////////////////////////////////
// Functions for SparseArray class
//////////////////////////////////////////////////////////////////////

// Here 
double &SparseArray::operator[](const int &m)
{ 
  // not 
  if(N)
	  if(fabs(Value[N])<=Limit) N--;

  // check to see if this element has already been stored
  // and return reference to if yes
  for(int n=1;n<=N;n++)
	  if(NonZero[n]==m) return (Value[n]);

  // add new element to sparse array
  N++;
  if(N>32)
	{
	  cerr << "SparseArray::operator[](const int &) :\t";
	  cerr << "Too many elements in SparseArray" << endl;
	  exit(1);
	}
  
  NonZero[N] = m;
  Value[N]   = 0.0; // initialize new element

  return (Value[N]);
}

ostream& operator<<(ostream& ostr, const SparseArray& a)
{
  for (int i = 1; i <= a.N; i++)
    ostr << a.NonZero[i] << " ";
  ostr << endl;

  for (int i = 1; i <= a.N; i++)
    ostr << a.Value[i] << " ";
  ostr << endl;

  return ostr;  
}